package thread;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello");
            }
        },5000);
    }

    public static void main2(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,10,10, TimeUnit.MINUTES,
                new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i = 0; i < 10000; i++) {
            int id = i;
            threadPoolExecutor.submit(() -> {
                System.out.println("消费元素：" + id);
            });
        }
        threadPoolExecutor.shutdown();
    }

    public static void main1(String[] args) throws InterruptedException {
        MyBlockingQueue queue = new MyBlockingQueue(10);
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                try {
                    queue.put(i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("生产元素：" + i);
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                while(true) {
                    try {
                        System.out.println("消费元素" + queue.take());
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        t1.start();
        Thread.sleep(20000);
        t2.start();
        t1.join();
        t2.join();
    }
}
